package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.domain.dao.IndexFilterDao;
import com.xbongbong.pro.domain.entity.IndexFilterEntity;
import com.xbongbong.saas.model.IndexFilterModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 首页看板筛选偏好配置
 * @Author: hongxiao
 * @date: 2019-02-27 16:18
 * @since v1.0
 */
@Service("indexFilterModel")
public class IndexFilterModelImple implements IndexFilterModel {
    @Resource
    private IndexFilterDao indexFilterDao;

    @Override
    public Integer insert(IndexFilterEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        return indexFilterDao.insert(entity);
    }

    @Override
    public Integer update(IndexFilterEntity entity) throws XbbException {
        entity.setUpdateTime((long)DateUtil.getInt());
        return indexFilterDao.update(entity);
    }

    @Override
    public Integer save(IndexFilterEntity entity) throws XbbException {
        if(entity.getId() == null || entity.getId().equals(0L)) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return indexFilterDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return indexFilterDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public IndexFilterEntity getByCorpidAndUserId(String corpid, String userId) {
        Map<String, Object> param = new HashMap<>(16);
        param.put("corpid", corpid);
        param.put("userId", userId);
        param.put("del", DelEnum.NORMAL.getDel());
        List<IndexFilterEntity> list = findEntitys(param);
        if(list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
